#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "analysisform.h"
#include "helpdialog.h"

#include <QMessageBox>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);
    ui->tabWidget->removeTab(1);
    ui->tabWidget->removeTab(0);
    connect(ui->actionAbout_PGLogAnalysis,SIGNAL(activated()),this,SLOT(showAboutDialog()));
    connect(ui->actionQuit,SIGNAL(triggered()),this,SLOT(quitApplication()));
    connect(ui->actionAdd_log_file,SIGNAL(triggered()),this,SLOT(addNewLogFile()));
    connect(ui->actionStart_new_analysis,SIGNAL(triggered()),this,SLOT(newAnalysis()));
    connect(ui->actionHelp,SIGNAL(triggered()),this,SLOT(showHelpDialog()));
    this->m_helpDialog = NULL;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showAboutDialog()
{
    QMessageBox::information(0,QString("About PostgreSQL Log Analysis"), QString("Version 0.7 \n\n\n\nCopyright:\nGunnar Lindholm\nkodapa@taljaren.se"));
}

void MainWindow::quitApplication()
{
    qDebug()<<"exiting the application...";
    close();
}

void MainWindow::newAnalysis()
{
    AnalysisForm* p = new AnalysisForm;
    ui->tabWidget->addTab(p,QString("Noname ")+QString::number(ui->tabWidget->count()));
    Analysis* tmpptr = new Analysis(p); //this); // send this so we can create modal dialogs
    if (tmpptr!=NULL)
    {
        tmpptr->doConnections();
        this->m_analysisList.append(tmpptr);
        this->activateAnalysisMenu(true);
        ui->tabWidget->setCurrentWidget(p);
    }
    else
    {
        qDebug()<<"Handle this error...";
    }
}

void MainWindow::openAnalysis()
{
}

void MainWindow::saveAnalysis()
{
}

void MainWindow::saveAnalysisAs()
{
}

void MainWindow::addNewLogFile()
{
    int index = ui->tabWidget->currentIndex();
    if (index >=0 && this->m_analysisList.count()> index && this->m_analysisList.at(index)!=NULL)
    {
        qDebug()<<"addnewlogfile... to analysis number "<<index;
        this->m_analysisList.at(index)->addNewLogFile();
    }
    else
        QMessageBox::warning(0,QString("No analysis started"), QString("You have to have an active analysis to be able to add a new logfile to the analysis."));
}

void MainWindow::activateAnalysisMenu(bool active)
{
    this->activateAddNewLogMenu(active);
    this->ui->menuAnalysis->setEnabled(active);
}

void MainWindow::activateAddNewLogMenu(bool active)
{
    this->ui->actionAdd_log_file->setEnabled(active);
}

void MainWindow::showHelpDialog()
{
    if (this->m_helpDialog == NULL)
    {
        this->m_helpDialog = new HelpDialog();
        this->m_helpDialog->show();
    }
    else
    {
        this->m_helpDialog->show();
        this->m_helpDialog->raise();
    }
}



